var express = require('express');
const router = express.Router();
//中间件
const resultResponeMiddleWare = require('../../MiddleWares/resultResponeMiddleWare');
const checkTokenMiddleWare = require('../../MiddleWares/checkTokenMiddleWare');
const ArticleModel = require('../../model/ArticleModel');

//发布文章
router.post('/add', checkTokenMiddleWare, resultResponeMiddleWare, (req, res, next) => {
    // console.log({...req.body});
    ArticleModel.create({ ...req.body }).then(data => {
        res.sendResponse(0, "ok")
    }).catch(err => {
        res.sendResponse(1, err)
    })
})

//修改文章


//删除文章


// 分页查询文章
router.get('/findArt', checkTokenMiddleWare, resultResponeMiddleWare, (req, res, next) => {
    //查询参数
    const query = req.query
    console.log(query);
    //查询第几页的数据，默认第一页
    const page = query.page || 1
    //每页数据条数
    const perPage = query.perPage || 10
    //总页数
    ArticleModel.countDocuments().then(total=>{
        console.log(res);
        ArticleModel.find({})
            .sort({ _id: -1 })
            .limit(perPage)//限制条数
            .skip(perPage * (page - 1))//第几页
            .then(result=>{
                // console.log(result);
                res.send({code:0,message:"ok",total,result})
            }).catch(err=>{
                res.sendResponse(1,err)
            })
    }).catch(err=>{
        console.log(err);
    })
})


module.exports = router